package net.oschina.arvin.dbjava.model;

import net.oschina.arvin.dbjava.util.ModelGenUtils;
import net.oschina.arvin.dbjava.util.Utils;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 反向工程 域模型属性定义
 *
 * @author Arvin
 * @time 2017/9/28 16:29
 */
public class ModelField {

    /**
     * 属性对应的java数据类型
     */
    private Class<?> javaType;

    /**
     * 属性名称
     */
    private String name;

    /**
     * 注释
     */
    private String comment;

    /**
     * 默认值
     */
    private String defaultValue;

    /** 是否是主键 */
    private boolean isPrimaryKey;

    /** 是否唯一 */
    private boolean isUnique;

    public ModelField(Class<?> javaType, String name, String comment) {
        if (Utils.isJavaKeyword(name)) {
            throw new RuntimeException("模型名称不能是java关键字： " + name);
        }
        this.javaType = javaType;
        this.name = name;
        this.comment = comment;
    }

    public ModelField() {
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public Class<?> getJavaType() {
        return javaType;
    }

    public void setJavaType(Class<?> javaType) {
        this.javaType = javaType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public boolean isPrimaryKey() {
        return isPrimaryKey;
    }

    public void setIsPrimaryKey(boolean isPrimaryKey) {
        this.isPrimaryKey = isPrimaryKey;
    }

    public boolean isUnique() {
        return isUnique;
    }

    public void setIsUnique(boolean isUnique) {
        this.isUnique = isUnique;
    }

    public String toImportCode() {
        // 基本的不需要import， Date类型需要
        if (this.javaType == java.util.Date.class) {
            return "import java.util.Date;";
        } else if (this.javaType == java.sql.Date.class) {
            return "import java.sql.Date;";
        } else if (this.javaType == java.sql.Time.class) {
            return "import java.sql.Time;";
        } else if (this.javaType == java.sql.Timestamp.class) {
            return "import java.sql.Timestamp;";
        } else if (this.javaType == BigInteger.class) {
            return "import java.math.BigInteger;";
        } else if (this.javaType == BigDecimal.class) {
            return "import java.math.BigDecimal;";
        }
        return null;
    }

    public String toFieldCode() {
        StringBuilder builder = new StringBuilder(ModelGenUtils.BLANK_4);
        String displayComment = comment;
        if (isPrimaryKey()) {
            displayComment = "主键 " + displayComment;
        } else if (isUnique()) {
            displayComment = "唯一 " + displayComment;
        }
        if (Utils.isNotBlank(displayComment)) {
            builder.append("/** ").append(displayComment).append(" **/\n").append(ModelGenUtils.BLANK_4);
        }
        builder.append("private ").append(this.javaType.getSimpleName()).append(" ").append(this.name);

        String defVal = toDefaultValue();
        if (Utils.isNotBlank(defVal)) { // 有默认值
            builder.append(" = ").append(defVal);
        }
        builder.append(";");

        return builder.toString();
    }

    /**
     * 转成默认值
     *
     * @return
     */
    private String toDefaultValue() {

        if (Utils.isNotBlank(defaultValue)) {
            if (String.class == this.javaType) {
                return "\"" + defaultValue + "\"";
            } else if (Long.class == this.javaType || long.class == this.javaType) {
                return defaultValue + "L";
            }
            return defaultValue;
        }
        return null;
    }

    public String toGetterSetterCode() {
        return toGetterMethodCode() + "\n\n" + toSetterMethodCode();
    }

    public String toGetterMethodCode() {
        StringBuilder builder = new StringBuilder(ModelGenUtils.BLANK_4);
        builder.append("public ").append(this.javaType.getSimpleName()).append(" ");
        if (this.javaType == boolean.class) {
            builder.append("is");
        } else {
            builder.append("get");
        }
        builder.append(Utils.firstLetterUpperCase(name)).append("() {\n");
        builder.append(ModelGenUtils.BLANK_8);
        builder.append("return ").append(name).append(";\n");
        builder.append(ModelGenUtils.BLANK_4).append("}");
        return builder.toString();
    }

    public String toSetterMethodCode() {
        StringBuilder builder = new StringBuilder(ModelGenUtils.BLANK_4);
        builder.append("public void set");
        builder.append(Utils.firstLetterUpperCase(name)).append("(");
        builder.append(this.javaType.getSimpleName()).append(" ").append(name).append(") {\n");
        builder.append(ModelGenUtils.BLANK_8);
        builder.append("this.").append(name).append(" = ").append(name).append(";\n");
        builder.append(ModelGenUtils.BLANK_4).append("}");
        return builder.toString();
    }
}
